﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ImportPiket
{
    /// <summary>
    /// Логика взаимодействия для ImportPage4.xaml
    /// </summary>
    public partial class ImportPage4 : Page
    {
        /// <summary>
        /// Флаг - инициализация формы
        /// </summary>
        bool isInit;
        public ImportPage4()
        {
            isInit = false;
            InitializeComponent();
            isInit = true;
            PrjProperty.pikets = new List<Sources.PiketString>();
            gridPikets.ItemsSource = PrjProperty.pikets;
            ReadAurigaFormat();
            gridPikets.Rebind();
        }


        void ReadAurigaFormat()
        {
            string stext = PrjProperty.txtData + "\r\n";
            using (System.IO.StringReader reader = new System.IO.StringReader(stext))
            {
                string line;
                var listValue = new List<string>();
                var piket = new Sources.PiketString();
                //читает текст по строчно
                while ((line = reader.ReadLine()) != null)
                {
                    line += " ";
                    string str = "";
                    listValue = new List<string>();
                    foreach (char ch in line)
                    {
                        if (ch != ' ')
                        {
                            str += ch.ToString();
                        }
                        else
                        {
                            if (str != "")
                            {
                                listValue.Add(str);
                                str = "";
                            }
                        }
                    }
                    piket = new Sources.PiketString();
                    for (int i = 0; i < listValue.Count ; i++)
                    {

                        if (i < 9)
                        {
                            switch (PrjProperty.fieldsList[i])
                            {
                                case "От":
                                    piket.From = listValue[i];
                                    break;
                                case "До":
                                    piket.To = listValue[i];
                                    break;
                                case "Длина":
                                    piket.Length = listValue[i];
                                    break;
                                case "Азимут":
                                    piket.Bearing = listValue[i];
                                    break;
                                case "Угол":
                                    piket.Inc = listValue[i];
                                    break;
                                case "Лево":
                                    piket.Left = listValue[i];
                                    break;
                                case "Верх":
                                    piket.Up = listValue[i];
                                    break;
                                case "Низ":
                                    piket.Down = listValue[i];
                                    break;
                                case "Право":
                                    piket.Right = listValue[i];
                                    break;
                            }
                        }
                        else
                        {
                            piket.Desc += listValue[i] + " ";
                        }
                    }//for (int i = 0; i < listValue.Count - 1; i++)
                    piket.IsPiket = PrjProperty.IsPiket(piket);
                    PrjProperty.pikets.Add(piket);
                }// while ((line = reader.ReadLine()) != null)

            }//using (System.IO.StringReader reader = new System.IO.StringReader(stext))
        }
    }
}
